package com.demo.function.orika;

import com.alibaba.fastjson.JSON;
import com.demo.function.orika.entity.Generics;
import com.demo.function.orika.entity.OrikaDestination;
import com.demo.function.orika.entity.OrikaSource;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.metadata.TypeBuilder;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class OrikaService {

    @Resource
    private MapperFacade mapperFacade;

    public OrikaDestination map(OrikaSource orikaSource) {
        OrikaDestination orikaDestination = mapperFacade.map(orikaSource, OrikaDestination.class);
        log.info("orikaDestination:{}", JSON.toJSONString(orikaDestination));
        return orikaDestination;
    }

    /**
     * 泛型映射，将对象存放在一个支持泛型的对象里，然后映射成其他对象
     * @param orikaSource 源对象
     * @return 映射目标对象
     */
    public Generics<OrikaDestination> mapGenerics(OrikaSource orikaSource) {
        Generics<OrikaSource> generics = new Generics<>();
        generics.setData(orikaSource);

        Generics<OrikaDestination> orikaDestination = mapperFacade.map(generics,
                new TypeBuilder<Generics<OrikaSource>>() {}.build(),
                new TypeBuilder<Generics<OrikaDestination>>() {}.build());
        log.info("Generics<OrikaDestination>:{}", JSON.toJSONString(orikaDestination));
        return orikaDestination;
    }
}
